package com.uzai.console.service.cms.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.UzaiRespVo;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.dto.cms.tbgoods.CmsMerTbGoodsInfoDto;
import com.uzai.console.dto.cms.tbgoods.CmsMerTbGoodsInfoListDto;
import com.uzai.console.dto.cms.tbgoods.CmsMerTbGoodsQuery;
import com.uzai.console.dto.cms.tbgoods.CmsTbGoodsQuery;
import com.uzai.console.dto.feign.uzaicms.CmsTbGoodsSuperSearchDto;
import com.uzai.console.dto.feign.uzaicms.CmsTbGoodsTypeSearchDto;
import com.uzai.console.entity.CmsMerTbGoods;
import com.uzai.console.mapper.CmsMerTbGoodsMapper;
import com.uzai.console.service.cms.CmsTbGoodsService;
import com.uzai.console.service.feign.UzaiCmsFeignService;
import com.uzai.console.vo.cms.tbgoods.CmsMerTbGoodsInfoVo;
import com.uzai.console.vo.cms.tbgoods.CmsTbGoodsFirstTypeVo;
import com.uzai.console.vo.cms.tbgoods.CmsTbGoodsInfoQueryResultVo;
import com.uzai.console.vo.cms.tbgoods.CmsTbGoodsInfoVo;
import com.uzai.console.vo.feign.uzaicms.SupperSearchVo;
import com.uzai.console.vo.feign.uzaicms.TypeListVo;
import com.uzai.console.vo.feign.uzaicms.TypeSearchVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author liuqi
 * @date 2021年08月23日 20:25
 */
@Service
public class CmsTbGoodsServiceImpl implements CmsTbGoodsService {

    @Autowired
    private CmsMerTbGoodsMapper cmsTbGoodsMapper;
    @Autowired
    private UzaiCmsFeignService uzaiCmsFeignService;

    /**
     * 分页查询CMS运营商配置的淘宝商品列表
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return CmsTbGoodsInfoVo
     */
    public Page<CmsMerTbGoodsInfoVo> findCmsMerTbGoodsList(CmsMerTbGoodsQuery cmsMerTbGoodsQuery){
        //新建返回到页面的LIST对象
        List<CmsMerTbGoodsInfoVo> cmsMerTbGoodsInfoVoArrayList = new ArrayList<>();
        //返回到页面分页对象
        Page<CmsMerTbGoodsInfoVo> pageVo = new Page<>();

        //执行分页查询对象
        Page<CmsMerTbGoods> page = new Page<>();
        cmsMerTbGoodsQuery.convert(page);
        List<CmsMerTbGoods> cmsTbGoodsList = cmsTbGoodsMapper.findByList(page, cmsMerTbGoodsQuery);

        if(cmsTbGoodsList != null && cmsTbGoodsList.size() > 0){
            for (CmsMerTbGoods cmsTbGoods : cmsTbGoodsList){
                CmsMerTbGoodsInfoVo cmsMerTbGoodsInfoVo = new CmsMerTbGoodsInfoVo();
                BeanUtils.copyProperties(cmsTbGoods, cmsMerTbGoodsInfoVo);
                cmsMerTbGoodsInfoVoArrayList.add(cmsMerTbGoodsInfoVo);
            }
        }

        BeanUtils.copyProperties(page, pageVo);
        pageVo.setRecords(cmsMerTbGoodsInfoVoArrayList);
        //将分页信息复制到返回到页面的分页MO当中
        return pageVo;
    }

    /**
     * 修改CMS运营商配置的淘宝商品详情
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param cmsMerTbGoodsInfoListDto
     * @return null
     */
    @Transactional
    public void updateCmsMerTbGoods(CmsMerTbGoodsInfoListDto cmsMerTbGoodsInfoListDto){
        //先清除原先的淘宝商品
        cmsTbGoodsMapper.deleteByMerId(cmsMerTbGoodsInfoListDto.getMerId());
        //在添加淘宝商品
        List<CmsMerTbGoodsInfoDto> cmsMerTbGoodsInfoDtoList = cmsMerTbGoodsInfoListDto.getCmsMerTbGoodsInfoDtoList();
        if(cmsMerTbGoodsInfoDtoList != null && cmsMerTbGoodsInfoDtoList.size() > 0){
            Integer now = DateUtil.getNowTime();
            for (CmsMerTbGoodsInfoDto cmsMerTbGoodsInfoDto : cmsMerTbGoodsInfoDtoList){
                CmsMerTbGoods cmsMerTbGoods = new CmsMerTbGoods();
                BeanUtils.copyProperties(cmsMerTbGoodsInfoDto,cmsMerTbGoods);
                cmsMerTbGoods.setId(IdWorker.getId());
                cmsMerTbGoods.setMerId(cmsMerTbGoodsInfoListDto.getMerId());
                cmsMerTbGoods.setCreateTime(now);
                cmsMerTbGoods.setUpdateTime(now);
                cmsTbGoodsMapper.insertSelective(cmsMerTbGoods);
            }
        }
    }

    /**
     * 超级搜索查询官方大淘客商品列表
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return CmsTbGoodsInfoVo
     */
    public CmsTbGoodsInfoQueryResultVo findCmsTbGoodsList_superSearch(CmsTbGoodsQuery cmsTbGoodsQuery){


        CmsTbGoodsInfoQueryResultVo cmsTbGoodsInfoQueryResultVo = new CmsTbGoodsInfoQueryResultVo();

        //新建返回到页面的LIST对象
        List<CmsTbGoodsInfoVo> cmsTbGoodsInfoVoList = new ArrayList<>();
        //分页标识(默认从1开始)
        String minId = "1";

        //请求接口-超级搜索
        CmsTbGoodsSuperSearchDto cmsTbGoodsSuperSearchDto = new CmsTbGoodsSuperSearchDto();

        //关键字
        if(StringUtils.isNotBlank( cmsTbGoodsQuery.getKeyword())){
            cmsTbGoodsSuperSearchDto.setKeyword(cmsTbGoodsQuery.getKeyword());
        }
        //分页
        if(cmsTbGoodsQuery.getMinId() != null){
            cmsTbGoodsSuperSearchDto.setMin_id(cmsTbGoodsQuery.getMinId());
        }
        //每页条数
        if(cmsTbGoodsQuery.getBack() != null){
            cmsTbGoodsSuperSearchDto.setBack(cmsTbGoodsQuery.getBack());
        }

        if(cmsTbGoodsQuery.getMinPrice() != null){
            cmsTbGoodsSuperSearchDto.setMinPrice(cmsTbGoodsQuery.getMinPrice());
        }

        if(cmsTbGoodsQuery.getMaxPrice() != null){
            cmsTbGoodsSuperSearchDto.setMaxPrice(cmsTbGoodsQuery.getMaxPrice());
        }

        if(cmsTbGoodsQuery.getSort() != null){
            cmsTbGoodsSuperSearchDto.setSort(cmsTbGoodsQuery.getSort());
        }

        UzaiRespVo<SupperSearchVo> uzaiRespVo = uzaiCmsFeignService.superSearch(cmsTbGoodsSuperSearchDto);
        if(uzaiRespVo.getCode() == 200){
            if(uzaiRespVo.getData() != null){
                cmsTbGoodsInfoVoList = uzaiRespVo.getData().getData();
                minId = uzaiRespVo.getData().getMin_id();
            }
        }
        cmsTbGoodsInfoQueryResultVo.setRecords(cmsTbGoodsInfoVoList);
        cmsTbGoodsInfoQueryResultVo.setMinId(minId);
        //将分页信息复制到返回到页面的分页MO当中
        return cmsTbGoodsInfoQueryResultVo;
    }


    /**
     * 查询官方大淘客淘宝商品一级类目列表
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return CmsTbGoodsInfoVo
     */
    public List<CmsTbGoodsFirstTypeVo> findCmsTbGoodsFirstTypeList(){

        //新建返回到页面的LIST对象
        List<CmsTbGoodsFirstTypeVo> cmsTbGoodsFirstTypeVoList = new ArrayList<>();

        UzaiRespVo<List<CmsTbGoodsFirstTypeVo>> uzaiRespVo =  uzaiCmsFeignService.firstTypeList();
        if(uzaiRespVo.getCode() == 200){
            if(uzaiRespVo.getData() != null){
                cmsTbGoodsFirstTypeVoList = uzaiRespVo.getData();
            }
        }
        //将分页信息复制到返回到页面的分页MO当中
        return cmsTbGoodsFirstTypeVoList;
    }

    /**
     * 分类查询官方大淘客淘宝商品列表
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return CmsTbGoodsInfoVo
     */
    public CmsTbGoodsInfoQueryResultVo findCmsTbGoodsList_typeSearch(CmsTbGoodsQuery cmsTbGoodsQuery){

        CmsTbGoodsInfoQueryResultVo cmsTbGoodsInfoQueryResultVo = new CmsTbGoodsInfoQueryResultVo();

        //新建返回到页面的LIST对象
        List<CmsTbGoodsInfoVo> cmsTbGoodsInfoVoList = new ArrayList<>();
        //分页标识（默认从1开始）
        String minId = "1";

        //请求接口-分类搜索
        CmsTbGoodsTypeSearchDto cmsTbGoodsTypeSearchDto = new CmsTbGoodsTypeSearchDto();

        //一级类目
        if(cmsTbGoodsQuery.getCid() != null){
            cmsTbGoodsTypeSearchDto.setCid(cmsTbGoodsQuery.getCid());
        }
        //分页
        if(cmsTbGoodsQuery.getMinId() != null){
            cmsTbGoodsTypeSearchDto.setMin_id(cmsTbGoodsQuery.getMinId());
        }
        //每页条数
        if(cmsTbGoodsQuery.getBack() != null){
            cmsTbGoodsTypeSearchDto.setSize(cmsTbGoodsQuery.getBack());
        }

        if(cmsTbGoodsQuery.getMinPrice() != null){
            cmsTbGoodsTypeSearchDto.setMinPrice(cmsTbGoodsQuery.getMinPrice());
        }

        if(cmsTbGoodsQuery.getMaxPrice() != null){
            cmsTbGoodsTypeSearchDto.setMaxPrice(cmsTbGoodsQuery.getMaxPrice());
        }

        if(cmsTbGoodsQuery.getSort() != null){
            cmsTbGoodsTypeSearchDto.setSort(cmsTbGoodsQuery.getSort());
        }

        if(cmsTbGoodsQuery.getCid() != null){
            cmsTbGoodsTypeSearchDto.setCid(cmsTbGoodsQuery.getCid());
        }

        UzaiRespVo<TypeSearchVo> uzaiRespVo = uzaiCmsFeignService.typeSearch(cmsTbGoodsTypeSearchDto);
        if(uzaiRespVo.getCode() == 200){
            if(uzaiRespVo.getData() != null){
                cmsTbGoodsInfoVoList = uzaiRespVo.getData().getData();
                minId = uzaiRespVo.getData().getMin_id();
            }
        }
        cmsTbGoodsInfoQueryResultVo.setRecords(cmsTbGoodsInfoVoList);
        cmsTbGoodsInfoQueryResultVo.setMinId(minId);

        //将分页信息复制到返回到页面的分页MO当中
        return cmsTbGoodsInfoQueryResultVo;
    }

    /**
     * 查询官方大淘客商品详情
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return CmsTbGoodsInfoVo
     */
    public CmsTbGoodsInfoVo findCmsTbGoodsInfo(CmsTbGoodsQuery cmsTbGoodsQuery){

        //返回的对象
        CmsTbGoodsInfoVo cmsTbGoodsInfoVo = new CmsTbGoodsInfoVo();

        //新建返回到页面的LIST对象
        List<CmsTbGoodsInfoVo> cmsTbGoodsInfoVoList = new ArrayList<>();
        //分页标识
        String minId = "1";

        //请求接口-超级搜索
        CmsTbGoodsSuperSearchDto cmsTbGoodsSuperSearchDto = new CmsTbGoodsSuperSearchDto();

        //关键字
        if(StringUtils.isNotBlank(cmsTbGoodsQuery.getKeyword())){
            cmsTbGoodsSuperSearchDto.setKeyword(cmsTbGoodsQuery.getKeyword());
        }
        //分页
        if(cmsTbGoodsQuery.getMinId() != null){
            cmsTbGoodsSuperSearchDto.setMin_id(cmsTbGoodsQuery.getMinId());
        }
        //每页条数
        if(cmsTbGoodsQuery.getBack() != null){
            cmsTbGoodsSuperSearchDto.setBack(cmsTbGoodsQuery.getBack());
        }

        //运营商ID
        //Long merId  = cmsTbGoodsQuery.getMerId();
        //param.put("uid",cmsTbGoodsQuery.getMerId());


        UzaiRespVo<SupperSearchVo> uzaiRespVo = uzaiCmsFeignService.superSearch(cmsTbGoodsSuperSearchDto);
        if(uzaiRespVo.getCode() == 200){
            if(uzaiRespVo.getData() != null){
                cmsTbGoodsInfoVoList = uzaiRespVo.getData().getData();
            }
        }

        if(cmsTbGoodsInfoVoList != null && cmsTbGoodsInfoVoList.size() > 0){
            cmsTbGoodsInfoVo = cmsTbGoodsInfoVoList.get(0);
        }
        return cmsTbGoodsInfoVo;
    }

}
